package repository

import (
	"dataaysn/internal/model"

	"gorm.io/gorm"
)

type SSHConfigRepository interface {
	CreateConfig(config *model.SSHConfig) error
	GetConfig(id string) (*model.SSHConfig, error)
	UpdateConfig(config *model.SSHConfig) error
	DeleteConfig(id string) error
	ListConfigs() ([]model.SSHConfig, error)
	TestConnection(config *model.SSHConfig) error
}
type gormSSHConfigRepository struct {
	db *gorm.DB
}

func NewGormSSHConfigRepository(db *gorm.DB) SSHConfigRepository {
	return &gormSSHConfigRepository{db: db}
}

func (r *gormSSHConfigRepository) CreateConfig(config *model.SSHConfig) error {
	return r.db.Create(config).Error
}

func (r *gormSSHConfigRepository) GetConfig(id string) (*model.SSHConfig, error) {
	var config model.SSHConfig
	err := r.db.First(&config, id).Error
	return &config, err
}

func (r *gormSSHConfigRepository) UpdateConfig(config *model.SSHConfig) error {
	return r.db.Save(config).Error
}

func (r *gormSSHConfigRepository) DeleteConfig(id string) error {
	return r.db.Delete(&model.SSHConfig{}, id).Error
}

func (r *gormSSHConfigRepository) ListConfigs() ([]model.SSHConfig, error) {
	var configs []model.SSHConfig
	err := r.db.Find(&configs).Error
	return configs, err
}

func (r *gormSSHConfigRepository) TestConnection(config *model.SSHConfig) error {
	// 实现SSH连接测试逻辑
	return nil
}
